import 'package:fish_redux/fish_redux.dart';
import 'package:kunlun_gas/app_files.dart';
import 'action.dart';
import 'state.dart';
import '../../model/main_view_model.dart';

Effect<SearchResultViewState> buildEffect() {
  return combineEffects(<Object, Effect<SearchResultViewState>>{
    SearchResultViewAction.action: _onAction,
    Lifecycle.initState: _initState,
    SearchResultViewAction.onLoad: _onLoad,
  });
}

void _onAction(Action action, Context<SearchResultViewState> ctx) {}

void _initState(Action action, Context<SearchResultViewState> ctx) {
  _sendRequest(ctx, 1);
}

void _onLoad(Action action, Context<SearchResultViewState> ctx) async {
  await _sendRequest(ctx, action.payload);
}

_sendRequest(Context<SearchResultViewState> ctx, int page) async {
  if (ctx.state.type == 4) {
    ResultData res =
        await CommonNetWork.getInstallList(page, key: ctx.state.keyword);
    if (res.success) {
      List list = res.data;
      List items = list.map((e) => InstallItemModel.fromJson(e)).toList();
      ctx.dispatch(SearchResultViewActionCreator.onGetData(items, page));
    } else {
      CommonUtils.showErrorInfo(ctx.context, res.desc);
      ctx.dispatch(SearchResultViewActionCreator.onGetData([], page));
    }
    return;
  }
  if (ctx.state.type == 3) {
    UserInfoEntity userInfo = GlobalStore.store.getState().userInfo;
    ResultData res = await CommonNetWork.communityList(
      key: ctx.state.keyword,
      status: ctx.state.status,
    );
    if (res.success) {
      List list = res.data['data'];
      List items = list.map((e) => CommunityItemModel.fromJson(e)).toList();
      ctx.dispatch(SearchResultViewActionCreator.onGetData(items, page));
    } else {
      CommonUtils.showErrorInfo(ctx.context, res.desc);
      ctx.dispatch(SearchResultViewActionCreator.onGetData([], page));
    }
    return;
  }

  if (ctx.state.type == 2) {
    ResultData res = await CommonNetWork.checkList(
      ctx.state.communityID,
      ctx.state.planID,
      page: page,
      key: ctx.state.isBuildingNo ? null : ctx.state.keyword,
      scworkBuildingNo: ctx.state.isBuildingNo ? ctx.state.keyword : null,
    );
    if (res.success) {
      List list = res.data['data'];
      List items = list
          .map((e) => CheckListItemModel.fromJson(
              e, ctx.state.communityID, ctx.state.communityName))
          .toList();
      ctx.dispatch(SearchResultViewActionCreator.onGetData(items, page));
    } else {
      CommonUtils.showErrorInfo(ctx.context, res.desc);
      ctx.dispatch(SearchResultViewActionCreator.onGetData([], page));
    }
    return;
  }

  if (ctx.state.type == 1) {
    ResultData res = await CommonNetWork.workerList(
      key: ctx.state.keyword,
      page: page,
    );
    if (res.success) {
      List list = res.data['data'];
      List items = list.map((e) => LeaderItemModel.fromJson(e)).toList();
      ctx.dispatch(SearchResultViewActionCreator.onGetData(items, page));
    } else {
      CommonUtils.showErrorInfo(ctx.context, res.desc);
      ctx.dispatch(SearchResultViewActionCreator.onGetData([], page));
    }
    return;
  }

  if (ctx.state.type == 6) {
    ResultData res =
        await CommonNetWork.patrolAreaList(key: ctx.state.keyword, page: page);

    if (res.success) {
      List list = res.data['data'];
      List items = list.map((e) => PatrolAreaModel.fromJson(e)).toList();

      ctx.dispatch(SearchResultViewActionCreator.onGetData(items, page));
    } else {
      CommonUtils.showErrorInfo(ctx.context, res.desc);
      ctx.dispatch(SearchResultViewActionCreator.onGetData([], page));
    }
    return;
  }

  if (ctx.state.type == 7) {
    ResultData res = await CommonNetWork.patrolItemList(
      ctx.state.communityID,
      ctx.state.planID,
      page: page,
      key: ctx.state.keyword,
    );

    if (res.success) {
      List list = res.data['data'];
      List items = list
          .map((e) => PatrolOrderItemModel.fromJson(
                e,
                ctx.state.communityID,
                ctx.state.communityName,
              ))
          .toList();
      ctx.dispatch(SearchResultViewActionCreator.onGetData(items, page));
    } else {
      CommonUtils.showErrorInfo(ctx.context, res.desc);
      ctx.dispatch(SearchResultViewActionCreator.onGetData([], page));
    }
    return;
  }

  if (ctx.state.type == 8) {
    ResultData res = await CommonNetWork.fixOrderList(
      workId: GlobalStore.store.getState().userInfo.userid,
      page: page,
      key: ctx.state.keyword,
      status: ctx.state.status,
    );
    if (res.success) {
      List list = res.data['data'];
      List items = list.map((e) => FixListItemModel.fromJson(e)).toList();
      ctx.dispatch(SearchResultViewActionCreator.onGetData(items, page));
    } else {
      CommonUtils.showErrorInfo(ctx.context, res.desc);
      ctx.dispatch(SearchResultViewActionCreator.onGetData([], page));
    }
    return;
  }

  if (ctx.state.type == 12) {
    ResultData res = await CommonNetWork.engineerOrderList(
      workId: GlobalStore.store.getState().userInfo.userid,
      page: page,
      pageSize: 999,
      type: GlobalStore.store.getState().userInfo.isCustomer ? '6' : '1,7,8',
      key: ctx.state.keyword,
      status: ctx.state.status,
    );
    if (res.success) {
      List list = res.data['data'];
      List items = list.map((e) => FixListItemModel.fromJson(e)).toList();
      ctx.dispatch(SearchResultViewActionCreator.onGetData(items, page));
    }
    else {
      CommonUtils.showErrorInfo(ctx.context, res.desc);
      ctx.dispatch(SearchResultViewActionCreator.onGetData([], page));
    }
    return;
  }
}
